package day6;

import java.util.*;

public class Homework121 {
    public static void main(String[] args) {
            demo1();
        //demo2();
        //demo3();
    }
    public static void demo1(){
        List<User> usersList=new ArrayList<>();
        //User u1=new User();
        //UserInfo u=new UserInfo(1,"男",19);
        //usersList.add(1,"李四",);
        usersList.add(new User (1,"李四",new UserInfo(101,"男",18)));
        usersList.add(new User (1,"李四1",new UserInfo(101,"男",18)));
        usersList.add(new User (1,"李四2",new UserInfo(101,"男",18)));
        usersList.add(new User (1,"李四3",new UserInfo(101,"男",18)));
        usersList.add(new User (1,"李四4",new UserInfo(101,"男",18)));
        usersList.add(new User (1,"李四5",new UserInfo(101,"男",18)));
        usersList.add(new User (1,"李四6",new UserInfo(101,"男",18)));
        Iterator<User> it=usersList.iterator();
        while (it.hasNext()){
            User user=it.next();
            System.out.println("ID"+" "+user.id+" "+"姓名"+
                    " "+user.name+" "+"性别"+" "+user.userinfo.sex);
        }
    }
    public static void demo2(){
//        Map<String,Integer> map=new HashMap<>();
//        Scanner sc=new Scanner(System.in);
//        System.out.println("请输入商品名称和单价");
//        System.out.println("商品：");
//        String name=sc.next();
//        System.out.println("单价");
//        int price=sc.nextInt();
//        if(map.containsKey(name)){
//            map.get(name).addItem(price);
//        }
        Map<String, int[]> cart = new HashMap<>(); // 商品 -> [数量, 总价]
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.print("商品：");
            String name = sc.next();
            if (name.equals("结束")) break;
            System.out.print("单价：");
            int price = sc.nextInt();
            // 更新购物车
            if (cart.containsKey(name)) {
                int[] info = cart.get(name);
                info[0]++;
                info[1] += price;
            } else {
                cart.put(name, new int[]{1, price});
            }
        }
        System.out.println("\n商品名称   个数   总价");
        int total = 0;
        for (Map.Entry<String, int[]> e : cart.entrySet()) {
            System.out.printf(e.getKey(), e.getValue()[0], e.getValue()[1]);
            total += e.getValue()[1];
        }
        System.out.println("总价: " + total);
    }
    public static void demo3(){
        List<User> userList = new ArrayList<>();
        // 1. 先创建UserInfo对象
        UserInfo info1 = new UserInfo(101, "男", 25);
        UserInfo info2 = new UserInfo(102, "女", 30);
        UserInfo info3 = new UserInfo(103, "男", 22);
        UserInfo info4 = new UserInfo(104, "女", 28);
        UserInfo info5 = new UserInfo(105, "男", 35);
        UserInfo info6 = new UserInfo(106, "女", 27);
        UserInfo info7 = new UserInfo(107, "男", 31);
        UserInfo info8 = new UserInfo(108, "女", 24);
        UserInfo info9 = new UserInfo(109, "男", 29);
        UserInfo info10 = new UserInfo(110, "女", 26);
        // 2. 再创建User对象并关联UserInfo
        User user1 = new User(1, "张三", info1);
        User user2 = new User(2, "李四", info2);
        User user3 = new User(3, "王五", info3);
        User user4 = new User(4, "赵六", info4);
        User user5 = new User(5, "钱七", info5);
        User user6 = new User(6, "孙八", info6);
        User user7 = new User(7, "周九", info7);
        User user8 = new User(8, "吴十", info8);
        User user9 = new User(9, "郑十一", info9);
        User user10 = new User(10, "王十二", info10);
        userList.add(user1);
        userList.add(user2);
        userList.add(user3);
        userList.add(user4);
        userList.add(user5);
        userList.add(user6);
        userList.add(user7);
        userList.add(user8);
        userList.add(user9);
        userList.add(user10);
        Iterator<User> iterator = userList.iterator();
        System.out.println("ID\tName\tSex\tAge");
        System.out.println("------------------------");

        while (iterator.hasNext()) {
            User user = iterator.next();
            UserInfo info = user.getUserinfo();
            System.out.printf(user.name+user.userinfo.sex);

        }
    }
}


class User{
    int id;
    String name;
    UserInfo userinfo;

    public User() {
    }

    public User(int id, String name, UserInfo userinfo) {
        this.id = id;
        this.name = name;
        this.userinfo = userinfo;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public UserInfo getUserinfo() {
        return userinfo;
    }

    public void setUserinfo(UserInfo userinfo) {
        this.userinfo = userinfo;
    }
}
class UserInfo{
    int id;
    String sex;
    int age;

    public UserInfo() {
    }

    public UserInfo(int id, String sex, int age) {
        this.id = id;
        this.sex = sex;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
class Item {
    int count;
    int totalPrice;
    public Item(int price) { // 首次购买时初始化
        this.count = 1;
        this.totalPrice = price;
    }
    public void addItem(int price) { // 再次购买时调用
        this.count++;
        this.totalPrice += price;
    }
}
